package com.jianglin.controller;

import com.jianglin.util.JwtUtil;
import com.jianglin.vo.LoginUser;
import com.jianglin.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.web.bind.annotation.*;

/**
 * @author jianglin
 * 登录获取令牌
 */
@RestController
@RequestMapping("/auth")
@CrossOrigin(maxAge = 3600)
public class AuthController {
    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private JwtUtil jwtUtil;
    @PostMapping("/login")
    public R login(@RequestBody LoginUser user){
        authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(user.getUsername(),user.getPassword()));
        String token = jwtUtil.generateToken(user.getUsername());
        return R.ok("Bearer " + token);
    }
}
